import 'package:flutter_menu/biz/order/detail/option/options_controller.dart';
import 'package:flutter_menu/biz/order/detail/package/package_controller.dart';
import 'package:flutter_menu/biz/order/detail/package/options_package_dialog.dart';
import 'package:flutter_menu/biz/order/widget/option_delete.dart';
import 'package:flutter_menu/model/product_model.dart';
import 'package:flutter_menu/res/app_design.dart';
import 'package:get/get.dart';

import '../../widget/option_item.dart';

class GroupSelectable extends GetView<PackageController> {
  const GroupSelectable({
    super.key,
    required this.id,
    required this.title,
    required this.max,
    required this.specList,
    this.mulChoice = false,
  });

  final String title;
  final String id;
  final int max;
  final List<Food> specList;
  final bool mulChoice;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PackageController>(
      id: id,
      init: PackageController(options: specList),
      global: false,
      builder: (PackageController controller) {
        return Obx(() {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 规格选择
              Text(title,
                  style:
                      TextStyle(fontSize: 26.sp, color: AppColors.text_CA7A2A)),

              GridView.count(
                physics: const NeverScrollableScrollPhysics(),
                crossAxisCount: 2,
                crossAxisSpacing: 34.w,
                mainAxisSpacing: 20.w,
                childAspectRatio: 3.5,
                shrinkWrap: true,
                children: controller.combineList.map((item) {
                  return OptionItem(
                    label: item.name ?? "",
                    subLabel: item.spec.name ?? "",
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    isEnable: true,
                    isIntrinsicWidth: false,
                    isSelected: false,
                    onTap: () {
                      handleProductClick(controller, item);
                    },
                  ).paddingOnly(top: 12.w);
                }).toList(),
              ),
              buildSelectedFoods(controller)
            ],
          );
        });
      },
    );
  }

  ///分类名称（n选m）：n个菜里支持选择其中m个，当选的数量大于m个时，再选择其他菜品，点击无反应，需取消其中某个已选菜品，才能换菜
  void handleProductClick(
      PackageController controller, CombineFoodWithSpec data) {
    debugPrint("handleProductClick: $data");
    String? productId = data.food.productId;
    var exceedLimit = controller.isProductExceedLimit(id, productId, max);
    if (exceedLimit) {
      debugPrint("handleProductClick: exceedLimit");
      return;
    }

    // 若选择的菜品有选项，弹出菜品选项选择弹框
    var options = data.spec.options;
    if (options.isNotEmpty) {
      Get.dialog(
        OptionsPackageDialog(data),
        transitionCurve: Curves.easeIn,
        transitionDuration: const Duration(milliseconds: 100),
      );
    } {
      // TODO 直接添加菜品数量
    }
  }

  buildSelectedFoods(PackageController controller) {
    return Padding(
      padding: const EdgeInsets.only(top: 6),
      child: controller
          .getSelectedProductByGroupId(id)
          .map((item) {
            return OptionDelete(
              label: item.count > 1 ? "${item.name}x${item.count}" : item.name,
              onTap: () {
                controller.removeProductInGroup(id, item.id);
              },
            ).paddingSymmetric(vertical: 7.w);
          })
          .toList()
          .toColumn(),
    );
  }
}
